package com.lyxedu.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyxedu.management.Services.StudentServie;
import com.lyxedu.management.handler.R;
import com.lyxedu.management.pojo.Student;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("student")
@RestController
@Api(tags = "学生相关接口")
public class StudentController {
    @Resource
    private StudentServie studentServie;

    /**
     * 获取学生列表
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取学生列表")
    public R<List<Student>> list(
            @RequestParam(value = "page",required = false,defaultValue = "1") int page,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10") int pageSize
    ){
        Page<Student> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Student::getDeleted,false);
        studentServie.page(pageInfo,wrapper);
        return R.success(pageInfo.getRecords());
    }

    /**
     * 通过名字来搜索
     * @param page
     * @param pageSize
     * @param key
     * @return
     */
    @GetMapping("/list/keywords")
    @ApiOperation(value = "搜索学生列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key",value = "学生姓名关键字",required = false)
    })
    public R<List<Student>> listKey(
            @RequestParam(value = "page",defaultValue = "1") int page,
            @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
            String key
    ){
        Page<Student> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Student::getDeleted,false);
        wrapper.like(key != null,Student::getName,key);
        studentServie.page(pageInfo,wrapper);
        return R.success(pageInfo.getRecords());
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除学生")
    public R<String> delete(int id){
        Student student = new Student();
        student.setId(id);
        student.setDeleted(true);
        boolean b = studentServie.updateById(student);
        if (b){
           return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改学生信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "student",value = "学生信息",required = false)
    })

    public R<String> delete(@RequestBody Student student){
        boolean b = studentServie.updateById(student);
        if (b){
            return R.success("修改成功");
        }
        return R.error("删除失败");
    }
}
